<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="./vue.js"></script>
    </head>
    <style>

    </style>
    <body>
        <div id="app">
            <cpn></cpn>
        </div>
    </body>
    <template id="cpnC">
        <div>
            <p>我是子组件</p>
            <ccpn-c></ccpn-c>
        </div>
    </template>
    <template id="ccpnC">
        <div>
            <p>我是ccpnC子组件</p>
            <button @click="btnclick">按钮</button>
        </div>
    </template>
    <script>
        let app = new Vue({
            el:"#app",
            data:{
                msg:"你好啊"
            },
            components: {
                cpn:{
                    template:"#cpnC",
                    data () {
                        return {
                            name:"我是子组件的name"      
                        }
                    },
                    components: {
                        ccpnC:{
                            template:"#ccpnC",
                            methods: {
                                btnclick(){
                                    // 1.访问父组件$parent
                                    console.log(this.$parent);// VueComponent
                                    console.log(this.$parent.name); // 我是子组件的name

                                    // 2.访问根组件$root
                                    console.log(this.$root); //Vue 
                                    console.log(this.$root.msg); //你好啊 
                                }
                            }
                        } 
                    }
                }
            }
        })
    </script>
</html> 